home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / edgetext / clsconfi.cl_ / clsconfi.cl
Encoding:
Visual Basic class definition  |  1998-06-28  |  30.5 KB  |  780 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = 0   'False
  4. END
  5. Attribute VB_Name = "clsConfiguration"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. '********************************************************************************************************
  12. 'Title:     clsConfiguration
  13. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  14. 'Purpose:   This class allows easy access to the registry for program configuration
  15. '           I also contains several very useful public methods for string manipulation,
  16. '           INI file access, and miscellaneous.
  17. '********************************************************************************************************
  18.  
  19. '----------------------------------------
  20. 'This class requires the Configuration Form
  21. 'which is also generated from the designer
  22. '----------------------------------------
  23. 'Here are some registry Constants
  24. Private Const REG_SZ As Long = 1
  25. Private Const REG_DWORD As Long = 4
  26. 'List Box and Combo Box constants
  27. Private Const LB_FINDSTRING = &H18F
  28. Private Const LB_FINDSTRINGEXACT = &H1A2
  29. Private Const CB_FINDSTRING = &H14C
  30. Private Const CB_FINDSTRINGEXACT = &H158
  31.  
  32. Private Const HKEY_CLASSES_ROOT = &H80000000
  33. Private Const HKEY_CURRENT_USER = &H80000001
  34. Private Const HKEY_LOCAL_MACHINE = &H80000002
  35. Private Const HKEY_USERS = &H80000003
  36.  
  37. Private Const ERROR_NONE = 0
  38. Private Const ERROR_BADDB = 1
  39. Private Const ERROR_BADKEY = 2
  40. Private Const ERROR_CANTOPEN = 3
  41. Private Const ERROR_CANTREAD = 4
  42. Private Const ERROR_CANTWRITE = 5
  43. Private Const ERROR_OUTOFMEMORY = 6
  44. Private Const ERROR_INVALID_PARAMETER = 7
  45. Private Const ERROR_ACCESS_DENIED = 8
  46. Private Const ERROR_INVALID_PARAMETERS = 87
  47. Private Const ERROR_NO_MORE_ITEMS = 259
  48. Private Const KEY_ALL_ACCESS = &H3F
  49. Private Const REG_OPTION_NON_VOLATILE = 0
  50.  
  51. '--------------------------------------------------------------------------------------------------------------------------
  52. 'The following are declarations to read the registry directly
  53. Private Declare Function RegCloseKey Lib "advapi32.dll" _
  54.         (ByVal hKey As Long) As Long
  55. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
  56.         "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
  57.         ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions _
  58.         As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes _
  59.         As Long, phkResult As Long, lpdwDisposition As Long) As Long
  60. Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias _
  61.         "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
  62.         ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As _
  63.         Long) As Long
  64. Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias _
  65.         "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
  66.         String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
  67.         As String, lpcbData As Long) As Long
  68. Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias _
  69.         "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
  70.         String, ByVal lpReserved As Long, lpType As Long, lpData As _
  71.         Long, lpcbData As Long) As Long
  72. Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias _
  73.         "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As _
  74.         String, ByVal lpReserved As Long, lpType As Long, ByVal lpData _
  75.         As Long, lpcbData As Long) As Long
  76. Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias _
  77.         "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
  78.         ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As _
  79.         String, ByVal cbData As Long) As Long
  80. Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias _
  81.         "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, _
  82.         ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, _
  83.         ByVal cbData As Long) As Long
  84. Private Declare Function apiWritePrivateProfileString Lib "kernel32" _
  85.         Alias "WritePrivateProfileStringA" (ByVal lpApplicationName _
  86.         As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
  87.         ByVal lpFileName As String) As Long
  88. Private Declare Function apiGetPrivateProfileString Lib "kernel32" _
  89.        Alias "GetPrivateProfileStringA" (ByVal lpApplicationName _
  90.        As String, ByVal lpKeyName As Any, ByVal lpDefault As _
  91.        String, ByVal lpReturnedString As String, ByVal nSize As _
  92.        Long, ByVal lpFileName As String) As Long
  93.  
  94. Private Declare Function GetUserName Lib "advapi32.dll" _
  95.        Alias "GetUserNameA" (ByVal lpBuffer _
  96.        As String, nSize As Long) As Long
  97. Private Declare Function SendMessage Lib "user32" _
  98.        Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
  99.        ByVal wParam As Long, lParam As Any) As Long
  100.  
  101. Private Type ConfigType
  102.     Name As String
  103.     Type As String
  104.     Data As String
  105. End Type
  106.  
  107. Public DataSource As String
  108. Public Connect As String
  109. Public LanId As String
  110. Public ModuleName As String
  111. Public LogonName As String
  112. Public Password As String
  113. Public DisplayErrors As String
  114. Public LogErrors As String
  115. Public LogFile As String
  116. Public DebugFlag As Integer
  117. Private gConfigItems As Long
  118. Private gConfigArray() As ConfigType
  119. Private gConfigItem As Long
  120.  
  121. Public Static Property Get NameValue() As String
  122.     NameValue = gConfigArray(gConfigItem).Name
  123. End Property
  124.  
  125. Public Static Property Let NameValue(pNameValue As String)
  126.     gConfigArray(gConfigItem).Name = pNameValue
  127. End Property
  128.  
  129. Public Static Property Get TypeValue() As String
  130.     TypeValue = gConfigArray(gConfigItem).Type
  131. End Property
  132.  
  133. Public Static Property Let TypeValue(pTypeValue As String)
  134.     gConfigArray(gConfigItem).Type = pTypeValue
  135. End Property
  136.  
  137. Public Static Property Get DataValue() As String
  138.     DataValue = gConfigArray(gConfigItem).Data
  139. End Property
  140.  
  141. Public Static Property Let DataValue(pDataValue As String)
  142.     gConfigArray(gConfigItem).Data = pDataValue
  143. End Property
  144.  
  145. Public Static Property Get ConfigItem() As Long
  146.     ConfigItem = gConfigItem
  147. End Property
  148.  
  149. Public Static Property Let ConfigItem(pConfigItem As Long)
  150.     gConfigItem = pConfigItem
  151. End Property
  152.  
  153. Public Static Property Get ConfigItems() As Long
  154.     ConfigItems = gConfigItems
  155. End Property
  156.  
  157. Public Static Property Let ConfigItems(pConfigItems As Long)
  158.     gConfigItems = pConfigItems
  159. End Property
  160.  
  161. Public Sub FillList(List As Control)
  162.  
  163. Dim I As Integer
  164.  
  165.     For I = 0 To gConfigItems - 1
  166.         List.AddItem gConfigArray(I).Name
  167.     Next I
  168.  
  169. End Sub
  170.  
  171. '********************************************************************************************************
  172. 'Title:     Save
  173. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  174. 'Purpose:   Saves the Data in the Array to the Registry
  175. 'Parameters:None
  176. 'Return:    Nothing
  177. '********************************************************************************************************
  178. '
  179. Public Sub Save()
  180.  
  181. Dim I As Long
  182.  
  183.     FillArray
  184.  
  185.     For I = 0 To gConfigItems - 1
  186.         If Trim(UCase(gConfigArray(I).Name)) = "PASSWORD" Then
  187.             If RegistryWriteData("Timesheet", Trim(gConfigArray(I).Name), Encrypt(Trim(gConfigArray(I).Data))) = False Then
  188.                 Screen.MousePointer = vbNormal
  189.                 MsgBox "Error Writing file"
  190.             End If
  191.         Else
  192.             If RegistryWriteData("Timesheet", Trim(gConfigArray(I).Name), Trim(gConfigArray(I).Data)) = False Then
  193.                 Screen.MousePointer = vbNormal
  194.                 MsgBox "Error Writing file"
  195.             End If
  196.         End If
  197.     Next I
  198.  
  199. End Sub
  200.  
  201. '********************************************************************************************************
  202. 'Title:     Refresh
  203. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  204. 'Purpose:   Refreshes the Array with Data from the Registry then calls Fill Class to
  205. '           Fill the Class with the Data also
  206. 'Parameters:None
  207. 'Return:    Nothing
  208. '********************************************************************************************************
  209. '
  210. Public Sub Refresh()
  211.  
  212.     gConfigItems = 9
  213.     ReDim gConfigArray(8)
  214.     gConfigArray(0).Name = "DataSource"
  215.     gConfigArray(0).Type = "String"
  216.     If Trim(RegistryReadData("Timesheet", "DataSource")) = "" Then
  217.         gConfigArray(0).Data = "c:\Program Files\TimeSheet\TimeSheet.mdb"
  218.     Else
  219.         gConfigArray(0).Data = RegistryReadData("Timesheet", "DataSource")
  220.     End If
  221.     gConfigArray(1).Name = "Connect"
  222.     gConfigArray(1).Type = "String"
  223.     If Trim(RegistryReadData("Timesheet", "Connect")) = "" Then
  224.         gConfigArray(1).Data = "Access"
  225.     Else
  226.         gConfigArray(1).Data = RegistryReadData("Timesheet", "Connect")
  227.     End If
  228.     gConfigArray(2).Name = "LanId"
  229.     gConfigArray(2).Type = "String"
  230.     If Trim(RegistryReadData("Timesheet", "LanId")) = "" Then
  231.         gConfigArray(2).Data = "N/A"
  232.     Else
  233.         gConfigArray(2).Data = RegistryReadData("Timesheet", "LanId")
  234.     End If
  235.     gConfigArray(3).Name = "ModuleName"
  236.     gConfigArray(3).Type = "String"
  237.     If Trim(RegistryReadData("Timesheet", "ModuleName")) = "" Then
  238.         gConfigArray(3).Data = "TimeSheet"
  239.     Else
  240.         gConfigArray(3).Data = RegistryReadData("Timesheet", "ModuleName")
  241.     End If
  242.     gConfigArray(4).Name = "LogonName"
  243.     gConfigArray(4).Type = "String"
  244.     gConfigArray(4).Data = RegistryReadData("Timesheet", "LogonName")
  245.     gConfigArray(5).Name = "Password"
  246.     gConfigArray(5).Type = "String"
  247.     gConfigArray(5).Data = Decrypt(RegistryReadData("Timesheet", "Password"))
  248.     gConfigArray(6).Name = "DisplayErrors"
  249.     gConfigArray(6).Type = "Boolean"
  250.     If Trim(RegistryReadData("Timesheet", "DisplayErrors")) = "" Then
  251.         gConfigArray(6).Data = "True"
  252.     Else
  253.         gConfigArray(6).Data = RegistryReadData("Timesheet", "DisplayErrors")
  254.     End If
  255.     gConfigArray(7).Name = "LogErrors"
  256.     gConfigArray(7).Type = "Boolean"
  257.     If Trim(RegistryReadData("Timesheet", "LogErrors")) = "" Then
  258.         gConfigArray(7).Data = "True"
  259.     Else
  260.         gConfigArray(7).Data = RegistryReadData("Timesheet", "LogErrors")
  261.     End If
  262.     gConfigArray(8).Name = "LogFile"
  263.     gConfigArray(8).Type = "String"
  264.     If Trim(RegistryReadData("Timesheet", "LogFile")) = "" Then
  265.         gConfigArray(8).Data = "c:\Program Files\Timesheet\TimeSheet.Log"
  266.     Else
  267.         gConfigArray(8).Data = RegistryReadData("Timesheet", "LogFile")
  268.     End If
  269.  
  270. FillClass
  271.  
  272. End Sub
  273.  
  274. '********************************************************************************************************
  275. 'Title:     FillArray
  276. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  277. 'Purpose:   Transfers data from the Class Variables to the Array
  278. 'Parameters:None
  279. 'Return:    Nothing
  280. '********************************************************************************************************
  281. '
  282. Public Sub FillArray()
  283.  
  284.     gConfigArray(0).Data = DataSource
  285.     gConfigArray(1).Data = Connect
  286.     gConfigArray(2).Data = LanId
  287.     gConfigArray(3).Data = ModuleName
  288.     gConfigArray(4).Data = LogonName
  289.     gConfigArray(5).Data = Password
  290.     gConfigArray(6).Data = DisplayErrors
  291.     gConfigArray(7).Data = LogErrors
  292.     gConfigArray(8).Data = LogFile
  293.  
  294. End Sub
  295.  
  296. '********************************************************************************************************
  297. 'Title:     FillClass
  298. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  299. 'Purpose:   Transfers data from the Array to the Class Variables
  300. 'Parameters:None
  301. 'Return:    Nothing
  302. '********************************************************************************************************
  303. '
  304. Public Sub FillClass()
  305.  
  306.     DataSource = gConfigArray(0).Data
  307.     Connect = gConfigArray(1).Data
  308.     LanId = gConfigArray(2).Data
  309.     ModuleName = gConfigArray(3).Data
  310.     LogonName = gConfigArray(4).Data
  311.     Password = gConfigArray(5).Data
  312.     DisplayErrors = gConfigArray(6).Data
  313.     LogErrors = gConfigArray(7).Data
  314.     LogFile = gConfigArray(8).Data
  315.  
  316. End Sub
  317.  
  318. '********************************************************************************************************
  319. 'Title:     Decrypt
  320. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  321. 'Purpose:   Decrypts the Password string stored in the INI file for AutoLogin
  322. 'Parameters:Password string to decrypt
  323. 'Return:    Decrypted Password string
  324. '********************************************************************************************************
  325. '
  326. Public Function Decrypt(Password As String) As String
  327.     
  328. Dim Count As Integer, Buf2 As String, Buf1 As String
  329.  
  330.     Buf1 = ZTrim(Password)
  331.     For Count = 1 To Len(Buf1)
  332.         Buf2 = Buf2 & Chr(Asc(Mid(Buf1, Count, 1)) - 10 - Count)
  333.     Next Count
  334.     Decrypt = Buf2
  335.  
  336. End Function
  337.  
  338. '********************************************************************************************************
  339. 'Title:     Encrypt
  340. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  341. 'Purpose:   Encrypts the Password string stored in the INI file for AutoLogin
  342. 'Parameters:Password string to Encrypt
  343. 'Return:    Encrypted Password string
  344. '********************************************************************************************************
  345. '
  346. Public Function Encrypt(Password As String) As String
  347.  
  348. Dim Count As Integer, Buf2 As String, Buf1 As String
  349.  
  350.     Buf1 = ZTrim(Password)
  351.     For Count = 1 To Len(Buf1)
  352.         Buf2 = Buf2 & Chr(Asc(Mid(Buf1, Count, 1)) + 10 + Count)
  353.     Next Count
  354.     Encrypt = Buf2
  355.  
  356. End Function
  357.  
  358. '********************************************************************************************************
  359. 'Title:     RegistryReadData
  360. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  361. 'Purpose:   Reads Data from the INI File Specified at the Key Location Specified
  362. 'Parameters:Progname Heading in the INI file,Key Sub-Heading in the INI file, and Filename of the INI file
  363. 'Return:    Retrieved INI Data
  364. '********************************************************************************************************
  365. '
  366. Public Function RegistryReadData(ByVal Progname, ByVal Key As String) As String
  367.  
  368. Dim ReturnString As String * 257, ReturnValue As Integer
  369. Dim Buf1 As String, TitleBuffer As String
  370.  
  371.     ReturnString = Space(257)
  372.     ReturnString = GetSetting(Progname, "SETTINGS", Key, "")
  373.     Buf1 = ReturnString
  374.     RegistryReadData = ZTrim(Buf1)
  375.  
  376. End Function
  377.  
  378. '********************************************************************************************************
  379. 'Title:     RegistryWriteData
  380. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  381. 'Purpose:   Writes Data to the INI File Specified at the Key Location Specified
  382. 'Parameters:Progname Heading in the INI file,Key Sub-Heading in the INI file, Data to be Written, and Filename of the INI file
  383. 'Return:    True if Successful, False if unsuccessful
  384. '********************************************************************************************************
  385. '
  386. Public Function RegistryWriteData(ByVal Progname, ByVal Key As String, ByVal KeyData As String) As Integer
  387.         
  388. Dim ReturnValue As Integer
  389.  
  390.     SaveSetting Progname, "SETTINGS", Key, KeyData
  391.         RegistryWriteData = True
  392.  
  393. End Function
  394.  
  395. '********************************************************************************************************
  396. 'Title:     ATrim  (Means All Trim)
  397. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  398. 'Purpose:   Trims Spaces ,Zeroes, CRs and LFs from the string passed in
  399. 'Parameters:String to be Trimmed
  400. 'Return:    Trimmed string
  401. '********************************************************************************************************
  402. '
  403. Public Function ATrim(ByVal Buf1 As String) As String
  404.     
  405.     Do While Left(Buf1, 1) = Chr(0) Or Left(Buf1, 1) = " " Or Left(Buf1, 1) = Chr(13) Or Left(Buf1, 1) = Chr(10)
  406.         Buf1 = Right(Buf1, Len(Buf1) - 1)
  407.     Loop
  408.     Do While Right(Buf1, 1) = Chr(0) Or Right(Buf1, 1) = " " Or Left(Buf1, 1) = Chr(13) Or Left(Buf1, 1) = Chr(10)
  409.         Buf1 = Left(Buf1, Len(Buf1) - 1)
  410.     Loop
  411.     ATrim = Buf1
  412.  
  413. End Function
  414.  
  415. '********************************************************************************************************
  416. 'Title:     ZTrim (Means Character Zero Trim)
  417. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  418. 'Purpose:   Trims Spaces and Zeroes from the string passed in. This is important because the INI strings
  419. '           returned are still in the C language traditional format with a trailing zero.
  420. 'Parameters:String to be Trimmed
  421. 'Return:    Trimmed string
  422. '********************************************************************************************************
  423. '
  424. Public Function ZTrim(ByVal Buf1 As String) As String
  425.  
  426.     
  427.     Do While Left(Buf1, 1) = Chr(0) Or Left(Buf1, 1) = Chr(32)
  428.         Buf1 = Right(Buf1, Len(Buf1) - 1)
  429.     Loop
  430.     Do While Right(Buf1, 1) = Chr(0) Or Right(Buf1, 1) = Chr(32)
  431.         Buf1 = Left(Buf1, Len(Buf1) - 1)
  432.     Loop
  433.     ZTrim = Buf1
  434.  
  435. End Function
  436.  
  437. '********************************************************************************************************
  438. 'Title:     PCase
  439. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  440. 'Purpose:   Changes strings to proper case
  441. 'Parameters:Buffer to be passed
  442. 'Return:    buffer to be set to proper case
  443. '********************************************************************************************************
  444.  
  445. Public Function PCase(ByVal psBuf As String) As String
  446.     
  447. Dim SpaceFlag As Integer
  448. Dim lsBuf As String
  449. Dim licount As Integer
  450.     
  451.     If Trim(psBuf) <> "" Then
  452.         For licount = 1 To Len(psBuf)
  453.            If Not SpaceFlag Then
  454.               lsBuf = lsBuf & UCase(Mid(psBuf, licount, 1))
  455.               SpaceFlag = True
  456.            Else
  457.               lsBuf = lsBuf & LCase(Mid(psBuf, licount, 1))
  458.            End If
  459.            If Mid(psBuf, licount, 1) = " " Then
  460.               SpaceFlag = False
  461.            End If
  462.         Next
  463.     End If
  464.     PCase = lsBuf
  465.     
  466. End Function
  467.  
  468. '********************************************************************************************************
  469. 'Title:     ReplaceChar
  470. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  471. 'Purpose:   This Function look for the specified char in the supplied buffer, and replace
  472. '           each instance of it in the buffer with the new character supplied
  473. 'Parameters:string to be modified, Old character to search for, and the new char to replace with
  474. 'Return:    the modified string
  475. '********************************************************************************************************
  476. Public Function ReplaceChar(psBuf As String, OldChar As String, NewChar As String) As String
  477.  
  478. Dim liStrLen As Integer
  479. Dim liCurChar As Integer
  480. Dim liOldCharPos As Integer
  481. Dim lsOutBuf As String
  482.  
  483.     liCurChar = 1
  484.     lsOutBuf = ""
  485.     
  486.     liOldCharPos = InStr(liCurChar, psBuf, OldChar)
  487.     If liOldCharPos = 0 Then
  488.         lsOutBuf = psBuf
  489.     Else
  490.         liStrLen = Len(psBuf)
  491.         Do While liOldCharPos > 0
  492.             lsOutBuf = lsOutBuf & Mid(psBuf, liCurChar, liOldCharPos - liCurChar) & NewChar
  493.             liCurChar = liOldCharPos + 1
  494.             liOldCharPos = InStr(liCurChar, psBuf, OldChar)
  495.         Loop
  496.         lsOutBuf = lsOutBuf & Mid(psBuf, liCurChar, liStrLen)
  497.     End If
  498.  
  499.     ReplaceChar = lsOutBuf
  500.  
  501. End Function
  502.  
  503. '********************************************************************************************************
  504. 'Title:     StripNP
  505. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  506. 'Purpose:   Strips Non Printable Characters from a buffer
  507. 'Parameters:Buffer to be passed
  508. 'Return:    Stripped buffer
  509. '********************************************************************************************************
  510.  
  511. Public Function StripNP(psRawBuf As String) As String
  512.  
  513. Dim licount As Integer
  514. Dim lsCleanBuf As String
  515.  
  516.     For licount = 1 To Len(psRawBuf)
  517.         If Asc(Mid(psRawBuf, licount, 1)) < 32 Or Asc(Mid(psRawBuf, licount, 1)) > 126 Then
  518.             lsCleanBuf = lsCleanBuf & " "
  519.         Else
  520.             lsCleanBuf = lsCleanBuf & Mid(psRawBuf, licount, 1)
  521.         End If
  522.     Next
  523.  
  524.     StripNP = lsCleanBuf
  525.  
  526. End Function
  527.  
  528. '********************************************************************************************************
  529. 'Title:     SetValueEx
  530. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  531. 'Purpose:   Puts values into the registry at the correct key location according to its type
  532. 'Parameters:key where value should be placed, SubKey Name where value is to be placed, type of variable
  533. '           and Value to be Placed in the registry
  534. 'Return:    success or fail
  535. '********************************************************************************************************
  536.  
  537. Private Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
  538.     
  539. Dim lValue As Long
  540. Dim sValue As String
  541.     
  542.     Select Case lType
  543.         Case REG_SZ
  544.             sValue = vValue & Chr(0)
  545.             SetValueEx = RegSetValueExString(hKey, sValueName, 0&, _
  546.                                            lType, sValue, Len(sValue))
  547.         Case REG_DWORD
  548.             lValue = vValue
  549.             SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, _
  550.             lType, lValue, 4)
  551.     End Select
  552. End Function
  553.  
  554. '********************************************************************************************************
  555. 'Title:     GetKeyValue
  556. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  557. 'Purpose:   Gets values from the registry at the correct key location
  558. 'Parameters:Type of variable,key where value is located, SubKey Name where value is to be placed,
  559. '           and the variable where the retrieved variable will be placed
  560. 'Return:    success or fail
  561. '********************************************************************************************************
  562.  
  563. Public Function GetKeyValue(ByVal lKeyType As Long, ByVal szKeyName As String, ByVal szValueName As String, vValue As Variant) As Long
  564.  
  565. Dim lhKey As Long
  566. Dim cch As Long
  567. Dim lrc As Long
  568. Dim lType As Long
  569. Dim lValue As Long
  570. Dim sValue As String
  571. Dim RetCode As Long
  572.  
  573.     On Error GoTo QueryValueExError
  574.     
  575.     lrc = RegOpenKeyEx(lKeyType, szKeyName, 0, KEY_ALL_ACCESS, lhKey)
  576.     If lrc <> ERROR_NONE Then Error 5
  577.  
  578.     ' Determine the size and type of data to be read
  579.     lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
  580.     If lrc <> ERROR_NONE Then Error 5
  581.  
  582.     Select Case lType
  583.         ' For strings
  584.         Case REG_SZ:
  585.             sValue = String(cch, 0)
  586.             lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
  587.             If lrc = ERROR_NONE Then
  588.                 vValue = Left(sValue, cch)
  589.             Else
  590.                 vValue = Empty
  591.             End If
  592.         ' For DWORDS
  593.         Case REG_DWORD:
  594.             lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
  595.             If lrc = ERROR_NONE Then vValue = lValue
  596.         Case Else
  597.             'all other data types not supported
  598.             lrc = -1
  599.     End Select
  600.     RegCloseKey (lhKey)
  601.  
  602. QueryValueExExit:
  603.     GetKeyValue = lrc
  604.     Exit Function
  605. QueryValueExError:
  606.     Resume QueryValueExExit
  607. End Function
  608.  
  609. '********************************************************************************************************
  610. 'Title:     SetKeyValue
  611. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  612. 'Purpose:   sets a key in the registry
  613. 'Parameters:Predefined Key such as HKEY_ROOT,key where value is located, SubKey Name where value is to be placed,
  614. '           and the new value of the key to be placed,and its type
  615. 'Return:    success or fail
  616. '********************************************************************************************************
  617.  
  618. Public Function SetKeyValue(lPredef As Long, sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long) As Long
  619.        
  620. Dim lRetVal As Long         'result of the SetValueEx function
  621. Dim hKey As Long         'handle of open key
  622.  
  623.     'open the specified key
  624.     lRetVal = RegOpenKeyEx(lPredef, sKeyName, 0, KEY_ALL_ACCESS, hKey)
  625.     lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
  626.     RegCloseKey (hKey)
  627.  
  628. End Function
  629.  
  630. '********************************************************************************************************
  631. 'Title:     CreateNewKey
  632. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  633. 'Purpose:   sets a new key into the registry
  634. 'Parameters:Predefined Key such as HKEY_ROOT,
  635. '           and the value of the new key to be placed
  636. 'Return:    success or fail
  637. '********************************************************************************************************
  638.  
  639. Public Function CreateNewKey(lPredefinedKey As Long, sNewKeyName As String) As Long
  640.  
  641. Dim hNewKey As Long         'handle to the new key
  642. Dim lRetVal As Long         'result of the RegCreateKeyEx function
  643.  
  644.    lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, _
  645.              vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, _
  646.              0&, hNewKey, lRetVal)
  647.    RegCloseKey (hNewKey)
  648.    
  649. End Function
  650.  
  651. '********************************************************************************************************
  652. 'Title:     INIReadData
  653. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  654. 'Purpose:   Reads data from an INI File
  655. 'Parameters:Bracketed INI Value,INI Key Value, and the INI FileName
  656. 'Return:    INI String
  657. '********************************************************************************************************
  658.  
  659. Public Function INIReadData(ByVal Progname, ByVal Key As String, ByVal Filename As String) As String
  660.  
  661. Dim ReturnString As String, ReturnValue As Long
  662. Dim RetCode As Integer
  663.  
  664.     ReturnString = Space(257)
  665.     RetCode = apiGetPrivateProfileString(Progname, Key, "", ReturnString, Len(ReturnString), Filename)
  666.     INIReadData = ZTrim(ReturnString)
  667.  
  668. End Function
  669.  
  670. '********************************************************************************************************
  671. 'Title:     INIWriteData
  672. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  673. 'Purpose:   Writes data to an INI File
  674. 'Parameters:Bracketed INI Value,INI Key Value, Data to be Placed, and the INI FileName
  675. 'Return:    Success or fail
  676. '********************************************************************************************************
  677.  
  678. Public Function INIWriteData(ByVal Progname, ByVal Key As String, ByVal KeyData As String, Filename As String) As Integer
  679.         
  680. Dim RetCode As Integer
  681.  
  682.     RetCode = apiWritePrivateProfileString(Progname, Key, KeyData, Filename)
  683.     INIWriteData = True
  684.  
  685. End Function
  686.  
  687. '********************************************************************************************************
  688. 'Title:     GetLANId
  689. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  690. 'Purpose:   Gets the LAN Id of the person on the Win 95 or Win NT Computer
  691. 'Parameters:none
  692. 'Return:    LAN ID
  693. '********************************************************************************************************
  694.  
  695. Public Function GetLANId() As String
  696.  
  697. Dim lsBuf As String
  698.  
  699.     lsBuf = Space(80) + Chr(0)
  700.     If GetUserName(lsBuf, 80) = False Then
  701.         MsgBox "Error Getting User Name", vbExclamation
  702.         GetLANId = ""
  703.         Exit Function
  704.     End If
  705.     If Trim(lsBuf) = "" Then
  706.         MsgBox "You Are Not Logged In to the LAN, Please Shutdown Your computer and Log on as a New User", vbInformation
  707.         GetLANId = ""
  708.         Exit Function
  709.     End If
  710.     GetLANId = UCase(ZTrim(lsBuf))
  711.  
  712. End Function
  713.  
  714. '********************************************************************************************************
  715. 'Title:     GetCBListIndex
  716. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  717. 'Purpose:   Finds a String in a Combo box and Returns the list index
  718. 'Parameters:Combo Box to Search, and String to Search For
  719. 'Return:    ListIndex where string is found, True if not found
  720. '********************************************************************************************************
  721. Public Function GetCBListIndex(pCombo As Control, ByVal psBuf As String) As Long
  722.  
  723.     GetCBListIndex = SendMessage(pCombo.hwnd, CB_FINDSTRINGEXACT, -1, ByVal psBuf)
  724.  
  725. End Function
  726.  
  727. '********************************************************************************************************
  728. 'Title:     GetLBListIndex
  729. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  730. 'Purpose:   Finds a String in a List box and Returns the list index
  731. 'Parameters:List Box to Search, and String to Search For
  732. 'Return:    ListIndex where string is found, True if not found
  733. '********************************************************************************************************
  734. Public Function GetLBListIndex(pList As Control, ByVal psBuf As String) As Long
  735.  
  736.     GetLBListIndex = SendMessage(pList.hwnd, LB_FINDSTRINGEXACT, -1, ByVal psBuf)
  737.  
  738. End Function
  739.  
  740. '********************************************************************************************************
  741. 'Title:     GetKeyListIndex
  742. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  743. 'Purpose:   Finds a String or Strings in an Array, then
  744. '           uses the GetCBListIndex function to return the listindex of the
  745. '           item in the box which fits the key
  746. 'Parameters:Combo Box to Search, Array to Search, Array with values to find
  747. 'Return:    ListIndex where string is found, True if not found
  748. '********************************************************************************************************
  749. Public Function GetKeyListIndex(pCombo As Control, SearchArray() As Variant, FindArray() As Variant) As Long
  750.  
  751. Dim liSearchCount As Long, liKeyCount As Integer
  752. Dim liMatchCount As Integer, liTotalKeys As Integer
  753.  
  754.     'Search the Array from 0 to its upper boundary
  755.     liTotalKeys = UBound(SearchArray(), 1)
  756.     For liSearchCount = 1 To UBound(SearchArray(), 2)
  757.         'Reset the Match Counter, and then look at every key for a match
  758.         liMatchCount = 0
  759.         For liKeyCount = 1 To liTotalKeys
  760.             'if a match is found, increment the counter which represents how many keys are a match
  761.             If SearchArray(liKeyCount, liSearchCount) = FindArray(liKeyCount, 1) Then
  762.                 liMatchCount = liMatchCount + 1
  763.             End If
  764.         Next
  765.         'if all the keys match, leave the loop
  766.         If liMatchCount = liTotalKeys Then
  767.             Exit For
  768.         End If
  769.     Next
  770.  
  771.     'if all keys matched, return the search array index that had the match
  772.     If liMatchCount = liTotalKeys Then
  773.         GetKeyListIndex = liSearchCount - 1
  774.     Else
  775.         'otherwise, just return true
  776.         GetKeyListIndex = True
  777.     End If
  778.  
  779. End Function
  780.